package com.example.himalayaproject.find.broadcast.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.broadcast.adapter.ProvinceTabAdapter;
import com.example.himalayaproject.find.broadcast.bean.ProvinceEntity;
import com.example.himalayaproject.find.broadcast.bean.ProvinceResult;
import com.example.himalayaproject.find.broadcast.callback.BroadcastHttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ProvinceActivity extends AppCompatActivity implements Callback<ProvinceEntity>, View.OnClickListener {
    private BroadcastHttpService service;
    private TextView typeTextView;
    private Intent intent;
    private String type;
    private int TAG;
    private TabLayout tab;
    private ViewPager pager;
    private List<ProvinceResult> datas;
    private ImageView backImageView;
    private ImageView moreImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_province);

        intent = getIntent();
        type = intent.getStringExtra("type");
        TAG = intent.getIntExtra("TAG",1);

        datas = new ArrayList<>();
        service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(BroadcastHttpService.class);
        service.getResult().enqueue(this);

        backImageView = (ImageView) findViewById(R.id.bct_province_back);
        backImageView.setOnClickListener(this);
        moreImageView = (ImageView) findViewById(R.id.bct_province_more);
        moreImageView.setOnClickListener(this);
        typeTextView = (TextView) findViewById(R.id.bct_province_type);
        typeTextView.setText(type);
        tab = (TabLayout) findViewById(R.id.bct_province_tab);
        pager = (ViewPager) findViewById(R.id.bct_province_pager);

    }

    @Override
    public void onResponse(Call<ProvinceEntity> call, Response<ProvinceEntity> response) {
        datas.addAll(response.body().getResult());
        pager.setAdapter(new ProvinceTabAdapter(getSupportFragmentManager(),datas));
        tab.setupWithViewPager(pager);
    }

    @Override
    public void onFailure(Call<ProvinceEntity> call, Throwable t) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bct_province_back:
                finish();
                break;
            case R.id.bct_province_more:
                Toast.makeText(this, "谁点这儿谁是臭傻逼、、、", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
